<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="width"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">
      <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="请选择要转移的目录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentId">
                  <a-select v-model="model.currentId" >
                    <a-select-option  v-for="(item,index) in this.parentFileList" :key="index" :value="item.id">{{item.fileName}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  export default {
    name: "reNameModal",
    data(){
      return{
        model:{},
        title:'',
        width: '80%',
        currentId:'',
        parentFileList:[],
        visible: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          currentId: [{ required: true, message: '请选择转移目录' }],
        },
        url: {
          edit: "/ghCustomerFileInfo/edit",
          queryList: "/ghCustomerFileInfo/queryList",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created() {
      this.loadData();
    },
    methods: {
      loadData(){
        getAction(this.url.queryList, {
          parentId: 0,
          pageNo: 1,
          pageSize: 50
        }).then((res) => {
          if (res.success) {
            this.parentFileList = res.result.records || res.result;
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      handleCancel() {
        this.visible = false;
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      handleOk () {
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let params = {};
            params.id = this.model.id;
            params.parentId = this.model.currentId;
            params.fileName = this.model.fileName;
            params.customerId = this.model.customerId;
            params.oldParentId = this.model.parentId;
            params.moveFileFlag = true;
            httpAction(httpurl, params, method).then((res)=>{
              if(res.success){
                this.$message.success(res.message);
                this.$emit('ok',this.model.parentId);
              }else{
                this.$message.warning(res.message);
              }
            }).finally(() => {
              this.confirmLoading = false;
              this.visible = false;
            })
          }
        })
      },
    }
  }
</script>
<style scoped>
</style>